<?php
namespace app\adm\controller;
use think\Controller;
use think\Db;
use think\Request;

/**
* 祭祀管理
*/
class Jisi extends Index{
	public function __construct(Request $request){
		parent::__construct($request);
	}
	//公共祭祀列表
	public function jisipub(){
		$page = Db::name('jisi')->where(['jisitype'=>'0'])->paginate(8,false,['type' => 'bootstrap','var_page' => 'page',]);
		$jisipub = $page->all();
		foreach ($jisipub as $key => $value) {
			$jisipub[$key]['nickname']=Db::name('user')->where(['userid'=>$jisipub[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('jisipub',$jisipub);
		return $this->fetch('jisipub',['title'=>'公共祭祀列表']);
	}
	// 私人祭祀列表
	public function jisipri(){
		$page = Db::name('jisi')->where(['jisitype'=>'1'])->paginate(8,false,['type' => 'bootstrap','var_page' => 'page',]);
		$jisipri = $page->all();
		foreach ($jisipri as $key => $value) {
			$jisipri[$key]['nickname']=Db::name('user')->where(['userid'=>$jisipri[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('jisipri',$jisipri);
		return $this->fetch('jisipri',['title'=>'私人祭祀列表']);
	}
	//删除祭祀
	public function jisidel(){
		$jisiid = input('jisiid');
		$res = Db::name('jisi')->delete($jisiid);
		if($res){
			jsBack('删除成功');
		}else{
			jsBack('删除失败');
		}
	}
	/**
	* 发表公共祭祀
	*/
	public function jisiadd(){
		return $this->fetch('jisiadd',['title'=>'公共祭祀发表']);
	}
	public function jisiInsert(){
		if(request()->isPost()){
			if(input('post.jisiname')=="" || input('post.jisicontent')==''){
				jsBack('标题和内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('jisiphoto');
			if($file){
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/jisi');
				if($info){
					$data['jisiphoto']='/public/uploads/jisi/'.date("Ymd").'/'.$info->getFilename();
					$data['jisiname']=input('post.jisiname');
					$data['jisicontent']=input('post.jisicontent');
					$data['addtime']=time();
					$data['userid']=session('userid');
					//公共祭祀类型为0
					$data['jisitype']='0';
					$jisi = Db::name('jisi')->insert($data);
					if($jisi){
						jsBack('添加成功','/adm/jisi/jisipub');
					}else{
						jsBack('网络有误,请重试');
					}
				}else{
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择祭祀展示图');
			}
		}else{
			$this->redirect('jisi/jisiadd');
		}
	}
	/**
	* 公共祭祀修改
	*/
	public function jisiedit()
	{
		$where['jisiid']=input('jisiid');
		$data = Db::name('jisi')->where(['jisiid'=>input('jisiid')])->find();
		$this->assign('data',$data);
		return $this->fetch('jisiedit',['title'=>'修改']);
	}
	public function jisiUpdate()
	{
		if(request()->isPost()){
			if(input('post.jisicontent')==''){
				jsBack('内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('jisiphoto');
			if($file){
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/jisi');
				if($info){
					$data['jisiphoto']='/public/uploads/jisi/'.date("Ymd").'/'.$info->getFilename();
					$data['jisicontent']=input('post.jisicontent');
					$data['addtime']=time();
					$jisiUpdate = Db::name('jisi')->where(['jisiid'=>input('jisiid')])->update($data);
					if($jisiUpdate){
						jsBack('修改成功','/adm/jisi/jisipub');
					}else{
						jsBack('修改失败');
					}
				}else{
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择祭祀展示图');
			}
		}else{
			$this->redirect('jisi/jisiedit');
		}
	}
}